package org.myspringframework.core.util;

/**
 * org.myspringframework.core.util <br>
 * 类型转换工具类
 *
 * @author jaymin
 * @since 2021/5/23
 */
public class ConvertUtil {

    /**
     * 返回基本类型的空值
     * 特殊处理: int/double/short/long/byte/float/boolean
     *
     * @param parameterType 参数类型
     * @return 对应的空值
     */
    public static Object primitiveNull(Class<?> parameterType) {
        if (isPrimitiveButNotBoolean(parameterType)) {
            return 0;
        } else if (parameterType == boolean.class) {
            return false;
        }
        return null;
    }

    private static boolean isPrimitiveButNotBoolean(Class<?> parameterType) {
        return parameterType == int.class || parameterType == double.class ||
                parameterType == short.class || parameterType == long.class ||
                parameterType == byte.class || parameterType == float.class;
    }

    /**
     * String类型转换成对应的参数类型
     *
     * @param parameterType 参数类型
     * @param requestValue  值
     * @return 转换后的Object
     */
    public static Object convert(Class<?> parameterType, String requestValue) {
        if (isPrimitive(parameterType)) {
            if (StringUtils.isEmpty(requestValue)) {
                return primitiveNull(parameterType);
            }
            if (parameterType.equals(int.class) || parameterType.equals(Integer.class)) {
                return Integer.parseInt(requestValue);
            } else if (parameterType.equals(String.class)) {
                return requestValue;
            } else if (parameterType.equals(double.class) || parameterType.equals(Double.class)) {
                return Double.parseDouble(requestValue);
            } else if (parameterType.equals(float.class) || parameterType.equals(Float.class)) {
                return Float.parseFloat(requestValue);
            } else if (parameterType.equals(long.class) || parameterType.equals(Long.class)) {
                return Long.parseLong(requestValue);
            } else if (parameterType.equals(short.class) || parameterType.equals(Short.class)) {
                return Short.parseShort(requestValue);
            } else if (parameterType.equals(boolean.class) || parameterType.equals(Boolean.class)) {
                return Boolean.parseBoolean(requestValue);
            } else if (parameterType.equals(byte.class) || parameterType.equals(Byte.class)) {
                return Byte.parseByte(requestValue);
            }
            return requestValue;
        } else {
            throw new RuntimeException("cannot support non primitive type conversion yet");
        }
    }

    private static boolean isPrimitive(Class<?> parameterType) {
        return parameterType.isPrimitive() ||
                parameterType.equals(Integer.class) ||
                parameterType.equals(Character.class) ||
                parameterType.equals(String.class) ||
                parameterType.equals(Long.class) ||
                parameterType.equals(Double.class) ||
                parameterType.equals(Float.class) ||
                parameterType.equals(Boolean.class) ||
                parameterType.equals(Byte.class) ||
                parameterType.equals(Short.class);
    }
}
